const Router = require('koa-router')
const axios = require('./utils/axios')
const utils = require('./utils')
const router = new Router({
  prefix: '/api/test'
})

router.get('/ipaddress', async (ctx) => {
  const ip = utils.getClientIp(ctx.req)
  const { data } = await axios.get(
    'http://ip.taobao.com/service/getIpInfo.php',
    {
      params: {
        ip: ip === '127.0.0.1' ? 'myip' : ip
      }
    }
  )
  ctx.body = {
    ...data
  }
})
router.get('/register', async (ctx) => {
  const { status, data } = await axios.post('/register', {
    validCode: '6501',
    email: '1057966749@qq.com',
    pwd: 'qian1234',
    rePwd: 'qian1234'
  })
  console.log(status, data)
  ctx.body = {
    code: 0
  }
})
router.get('/sendRegisterEmail', async (ctx) => {
  const { status, data } = await axios.post('/sendRegisterEmail', {
    params: '1057966749@qq.com'
  })
  console.log(status, data)
  ctx.body = {
    code: 0
  }
})
router.get('/session', (ctx) => {
  console.log(ctx.session)
  ctx.body = {
    code: 0
  }
})
router.get('/get', (ctx) => {
  const body = ctx.query ? ctx.query : {}
  console.log('测试接口接受GET请求')
  console.log('请求数据对象：')
  console.log(body)
  ctx.body = {
    code: 1,
    data: body
  }
})
router.post('/post', (ctx) => {
  const body = ctx.request.body ? ctx.request.body : {}
  console.log('测试接口接受POST请求')
  console.log('请求数据对象：')
  console.log(body)
  ctx.body = {
    code: 1,
    data: body
  }
})
router.put('/put', (ctx) => {
  const body = ctx.request.body ? ctx.request.body : {}
  console.log('测试接口接受PUT请求')
  console.log('请求数据对象：')
  console.log(body)
  ctx.body = {
    code: 1,
    data: body
  }
})
router.patch('/patch', (ctx) => {
  const body = ctx.request.body ? ctx.request.body : {}
  console.log('测试接口接受PATCH请求')
  console.log('请求数据对象：')
  console.log(body)
  ctx.body = {
    code: 1,
    data: body
  }
})
router.delete('/delete', (ctx) => {
  const body = ctx.request.body ? ctx.request.body : {}
  console.log('测试接口接受DELETE请求')
  console.log('请求数据对象：')
  console.log(body)
  ctx.body = {
    code: 1,
    data: body
  }
})
module.exports = router
